package turistic.GUI.graphic;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

//import com.sun.corba.se.impl.presentation.rmi.IDLTypeException;

//import DesEncrypter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TurisztikaiUtvonalTervezoActivity extends Activity {
	String uj="";
	String passStr;
	Connection conn;
	/** Called when the activity is first created. */
	@Override
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button loginB = (Button) findViewById(R.id.btnLogin);
		loginB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stu
				EditText user = (EditText) findViewById(R.id.loginEmail);
				String userStr = user.getText().toString();
			if (userStr.isEmpty())
				userStr = " ";

				EditText pass = (EditText) findViewById(R.id.loginPassword);
				passStr = pass.getText().toString();
				if (passStr.isEmpty())
					passStr = " ";
				   
				   try {
					    // Generate a temporary key. In practice, you would save this key.
					    // See also Encrypting with DES Using a Pass Phrase.
					   SecretKey key = KeyGenerator.getInstance("DES").generateKey();
						 // Create encrypter/decrypter class
					   Hash2 encrypter = new Hash2(key);
					    // Encrypt
					    String encrypted = encrypter.encrypt(passStr);
					    encrypted = encrypted.replace("\n","");
					    conn = new Connection("|"+"Lgnflg"+"|"+userStr+"|"+encrypted+"|");
					} catch (Exception e) {
					}
				
				    String mess = conn.getMess();
					
				    
				
				
				
				if (mess.equals("LgnSucesfull"))
				startActivity(new Intent(
						TurisztikaiUtvonalTervezoActivity.this,
						ParamActivity.class));
				else messageBox(mess);
				

			}
		});

		Button regB = (Button) findViewById(R.id.btnLinkToRegisterScreen);
		regB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(
						TurisztikaiUtvonalTervezoActivity.this,
						RegActivity.class));

			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			int pid = android.os.Process.myPid();
			android.os.Process.killProcess(pid);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void messageBox(String mess) {
		final AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();

		alertDialog.setTitle("Uzenet");
		alertDialog.setMessage(mess);

		alertDialog.show();

	}

}